In [10]:
import matplotlib
import numpy as np
import matplotlib.pyplot as plt

In [ ]:
%matplotlib inline

vectorization in numpy


In [11]:
import math
def sinckernel(x):
    if -0.01<x<0.01:
        return 1.0
    return math.sin(x)/x

In [12]:
v = np.arange(-5,5,0.1)

scalar function applied to a vector generates an error (as expected)


In [17]:
sincv = sinckernel(v)


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-17-859b1f123090> in <module>()
----> 1 sincv = sinckernel(v)

<ipython-input-11-f37756d81278> in sinckernel(x)
      1 import math
      2 def sinckernel(x):
----> 3     if -0.01<x<0.01:
      4         return 1.0
      5     return math.sin(x)/x

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

In [14]:
sinc = np.vectorize( sinckernel )

In [15]:
sincv = sinc( v )

In [16]:
plt.plot(v, sincv);



In [ ]: